<?php
//连数据库
require('../Common/db.php');
$bid = $_POST['bid'];
$title = trim($_POST['title']);
$tid = $_POST['tid'];
$wid=$_POST['wid'];
$pic = $_POST['pic'];
$intro = $_POST['intro'];
$address = $_POST['address'];
//上传文件错误的判定
if ($_FILES["pic"]["error"] > 0) {
    switch ($_FILES["pic"]["error"]) {
        case 1:
            echo "上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值";
            exit;
        case 3:
            echo "部分文件上传";
            exit;
        case 4:
            echo "没有选择海报!";
            //如果没选择图片，则编写更新其他数据的sql语句
            $sql = "update blogs set title='$title',tid=$tid,wid=$wid,uploaddate=now(),intro='$intro',address='$address' where bid=$bid";
            break;
        default:
            echo "未知错误";
            exit;
    }
} else {   //上传文件正常，更新数据表
//2.上传文件判断类型
    $allowtype = array("jpg", "jpeg", "png", "gif", "bmp", "flv");
    $arr = explode(".", $_FILES["pic"]["name"]);
    $suffix = $arr[count($arr) - 1];
    if (!in_array($suffix, $allowtype)) {
        echo "文件类型错误！";
        exit;
    } else {
        echo "<br>封面已上传<br>";
    }
//3.拷贝文件
    $filepath = "../../images/blog/";
    $randname = date("YmdHis") . "." . $suffix;
    move_uploaded_file($_FILES["pic"]["tmp_name"], $filepath . $randname);
    //获取原封面文件的文件名
    $sql = "select * from blogs where bid={$bid}";
    $result = mysql_query($sql);
    $row = mysql_fetch_assoc($result);
    $filename = $filepath . $row["pic"];
    //删除原来的头像文件
    if (file_exists($filename))
        unlink($filename);
    //编写SQL语句
    $sql = "update blogs set title='$title', tid=$tid,uploaddate=now(),intro='$intro',uploadadmin='$uploadadmin',address='$address',pic='$randname' where bid=$bid";
}
//执行SQL语句
$result = mysql_query($sql) or die("sql={$sql}, 更新失败！<br/>" . mysql_error());
//判断是否更新成功
$num = mysql_affected_rows();//返回前一次 MySQL 操作所影响的记录行数
if ($num != 1) {
    echo "更新失败,2秒后返回博客列表页";
    header("refresh:2;url='blogList.php'");
} else {
    header("location:blogList.php");
}
?>
